package com.luoxiang.socialandroid.activity;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.socialandroid.common.Constants.MmkvConst.KEY;
import static com.luoxiang.socialandroid.common.Constants.MmkvConst.TOKEN;
import static com.luoxiang.socialandroid.common.Constants.MmkvConst.USERID;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;

import com.alipay.mobile.android.verify.sdk.BizCode;
import com.alipay.mobile.android.verify.sdk.ServiceFactory;
import com.alipay.mobile.android.verify.sdk.interfaces.ICallback;
import com.alipay.mobile.android.verify.sdk.interfaces.IService;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.luoxiang.lib_common.utils.MmkvUtil;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.NetUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class FacawebContentActivity extends BaseActivity {
    private WebView mWv;
    private ImageView icon_back;
    IService mService;
    private String webcontent, cerid, name, idcard;
    private String token;

    @Override
    protected void initListener() {
        icon_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_facaweb_content);

    }

    @Override
    protected void initView() {

        // 初始化函数涉及获取设备信息，因此需要在隐私弹框之后调用，建议尽早初始化
        ServiceFactory.init(this);
        token = MmkvUtil.INSTANCE.decodeString(TOKEN);
        Intent intent = getIntent();

        cerid = intent.getStringExtra("cerid");
        name = intent.getStringExtra("namescard");
        idcard = intent.getStringExtra("idcard");
        // 保存 IService 实例
        mService = ServiceFactory.create(this).build();
        mWv = findViewById(R.id.web_loadface);
        icon_back = findViewById(R.id.icon_back);
        Map<String, Object> requestInfo = new HashMap<>();
        requestInfo.put("bizCode", BizCode.Value.FACE_APP);
        requestInfo.put("certifyId", cerid);
        webcontent = intent.getStringExtra("webcontent");

        mWv.loadUrl(webcontent);
        mService.startService(requestInfo, true, new ICallback() {
            @Override
            public void onResponse(Map<String, String> response) {
                // TODO with sdk response
                String s = response.toString();
                LogUtils.i("responsible", s);
                String resultStatus = response.get("resultStatus") + "";
                if (resultStatus.equals("9000")) {
                    //人脸录入成功
                    String userid = MmkvUtil.INSTANCE.decodeInt(USERID).toString();
                    JSONObject jsonObject = new JSONObject();
                    RequestBody requestBody = null;
                    try {
                        jsonObject.put("userId", userid);
                        jsonObject.put("name", name);
                        jsonObject.put("idCard", idcard);
                        requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), jsonObject.toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    getRealResult(token, EXTRA, requestBody, cerid);

                } else {
                    //人脸录入失败
                    ToastUtils.showShort(R.string.mi_shualianshibai);
                    finish();
                }
            }
        });

    }

    /**
     * 查询实名认证是否成功的方法
     */
    private void getRealResult(String token, String extra, RequestBody requestBody, String certifyId) {
        getRequest();
        NetUtils.getInstance().getApiService().getRealResult(token, extra, requestBody, certifyId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LogUtils.i("decryptStrdd", decryptStr);
                        ToastUtils.showShort(getString(R.string.mi_ziliaoshenhezhong));
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();

    }
}